//
//  PFCycleImages.m
//  06-CycleImages
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PFCycleImages.h"
@interface PFCycleImages () <UIScrollViewDelegate>
@property (nonatomic, strong)UIScrollView *cycleScrollView;
@property (nonatomic, strong)UIPageControl *imagesPage;
@end

@implementation PFCycleImages
#pragma mark - scrollView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
  // 改变pageControl的对应下标
   NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
    _imagesPage.currentPage = page;
    if (page == 0) {
  // 改变滚动视图的偏移量
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imagesArray.count - 2), 0);
        _imagesPage.currentPage = _imagesArray.count - 3;
    } else if (page == _imagesArray.count - 1) {
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _imagesPage.currentPage = 0;
    }
    
}
#pragma mark - initWith
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
      
        [self initCycleScrollView];
        [self initImagePage:frame];
        
    }
    return self;
}
- (void)initCycleScrollView {
    // 子控件初始化时，frame不要与初始化方法的frame一样
      // 推荐使用self.bounds / CGRectMake(0, 0, frame.size.width, frame.size.height)        
        _cycleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _cycleScrollView.backgroundColor = [UIColor blackColor];
        _cycleScrollView.pagingEnabled = YES;
        _cycleScrollView.bounces = NO;
        _cycleScrollView.delegate = self;
        _cycleScrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_cycleScrollView];
}
#warning  后期需要添加page的位置改变， 这里默认位置
- (void)initImagePage:(CGRect)frame {
    _imagesPage = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0,  frame.size.width / 3, 20)];
        _imagesPage.numberOfPages = 5;
        _imagesPage.center = CGPointMake(frame.size.width / 2, frame.size.height - 20 / 2);
        [self addSubview:_imagesPage];
    [_imagesPage addTarget:self action:@selector(imagesPageChange:) forControlEvents:UIControlEventValueChanged];
}
-  (void)imagesPageChange:(UIPageControl *)pageControl {
    [_cycleScrollView setContentOffset:CGPointMake((pageControl.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
    
}
#pragma mark - ImagesArray
//自定义方法
- (void)setImagesArrayWithArray:(NSArray *)array {
    //1根据传过来的数组， 进行处理， 生成图片数组
    NSArray *imagesArray = [self handleArray:array];
        //2 逻辑判断时， 需要用到数组内容
    self.imagesArray = imagesArray;
   //3 根据数组生成对应得ImageView
    [self createAllImageViewsWithImagesArray:imagesArray];
    // 根据图片数组， 设置对应得视图
    _imagesPage.numberOfPages = array.count;
_cycleScrollView.contentSize = CGSizeMake(imagesArray.count * self.bounds.size.width, 0);
   //因为添加了最后一张图， 要让轮播图看起来在第一张
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
}
- (void)createAllImageViewsWithImagesArray:(NSArray *)imagesArray {
    // 直接使用数组创建ImageView
    for (int i = 0; i < imagesArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imagesArray[i];
        [_cycleScrollView addSubview:imageView];
    }
}
- (NSArray *)handleArray:(NSArray *)array {
    //  判断类型，（不知道别人使用你的类的时候传进来的类型是什么）
    id firstObject = [array firstObject];
    // 最终的图片数组
    NSMutableArray *imagesArray = [NSMutableArray array];
    // 判断对象是不是某种类型
    if ([firstObject isKindOfClass:[NSString class]]) {
        // 把字符串数组 转换 成UIImage数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imagesArray addObject:image];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        [imagesArray setArray:array];
    }
    //获取到第一张图
    UIImage *firstImage = [imagesArray firstObject];
    //获取到最后一张图
    UIImage *lastImage = [imagesArray lastObject];
    // 将最后一张图放入下标为0的位置
    [imagesArray insertObject:lastImage atIndex:0];
    //  在最后添加第一张图
    [imagesArray addObject:firstImage];
    
    return [imagesArray copy];
}


@end
